package org.jetnuke.dao.generic;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.dao.PictureCommentDAO;
import org.jetnuke.entities.PictureComment;

public class GenericPictureCommentDAO extends AutoKeys implements PictureCommentDAO {

	public PictureComment retrieveByPK(int id) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PictureCommentModel.selectByID"));
		p.setInt(1, id);
		
		ResultSet rs = p.executeQuery();
		PictureComment  s = new PictureComment();
		
		rs.next() ;
		fillPictureCommentFromResultSet(s, rs);
		
		
		rs.close();
		p.close();

		return s;
	}

	protected void fillPictureCommentFromResultSet(PictureComment s, ResultSet rs) throws Exception{
		s.setMSG_AUTHOR(rs.getInt("MSG_AUTHOR"));
		s.setMSG_BODY(rs.getString("MSG_BODY"));
		s.setMSG_DATE(rs.getTimestamp("MSG_DATE"));
		s.setMSGID(rs.getInt("MSG_ID"));
		s.setPID(rs.getInt("PID"));		
	}


	protected List processSelectAll(ResultSet rs) throws Exception{
		List list = new ArrayList();
		while (rs.next()) {
			PictureComment u = new PictureComment();
			fillPictureCommentFromResultSet(u,rs);			
			list.add(u);
		}
		return list;
	}

	public int addNew(PictureComment pa) throws Exception {
		PreparedStatement p = this.getStatementForAutoKeys("PictureCommentModel.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals.getSql("PictureCommentModel.lastGeneratedPictureCommentID"));
		initPictureComment(pa, p);
		
		int id = this.executeAutoKeysQuery(p);
		p.close();		
		return id;
		
	}

	protected void initPictureComment(PictureComment pa, PreparedStatement p)throws Exception {
		GregorianCalendar gc=new GregorianCalendar();
		long initTime=gc.getTimeInMillis();
		Timestamp stamp=new Timestamp(initTime);
		
		p.setInt(1,pa.getMSG_AUTHOR());
		p.setString(2,pa.getMSG_BODY());
		p.setTimestamp(3,stamp);
		p.setInt(4,pa.getPID());

		
	}

	public List selectByPicture(int pid) throws Exception {
		PreparedStatement p= JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PictureCommentModel.selectByPicture"));
		p.setInt(1,pid);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

	public List selectLastComments(int limit) throws Exception {
		PreparedStatement p= JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PictureCommentModel.selectLastComments"));
		p.setInt(1,limit);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

	public void deleteByPID(int pid) throws Exception {
		PreparedStatement p= JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PictureCommentModel.deleteByPID"));
		p.setInt(1,pid);
		p.executeUpdate();
		p.close();
	}


}
